<?php
namespace Home\Controller;

class MessageController extends \Home\Controller\BaseController
{
	/**
	 * 查询消息
	 */
	public function messageList()
	{
		$param = I('params');
		$option = array();
////		$where = array();
////		if(I('data.page', '', '/^\d+$/', $param) && I('data.size', '', '/^\d+$/', $param)) {
////			$option['page'] = $param['page'];
////			$option['size'] = $param['size'];
////		}
////
////		$where['user_id'] = $this->user['userId'];
//		$message = D('Message')->searchMessage($where, $option);
		$message = M("GlobalNote")->where(array("super"=>'2'))->order("create_time desc")->select();
		$this->ajaxReturn($this->result($message));
	}
	
	/**
	 * 查看消息详情
	 */
	public function messageDetail()
	{
		$param = I('params');
		$id = I('data.id', '', '/^\d+$/', $param);
		if(!$id) $this->ajaxReturn(array('error' => 1, 'info' => '请传入消息id'));
		
		$messageModel = D('GlobalNote');
		//查询消息
		$message = $messageModel->where(array('id' => $id, 'status' => array('gt', 0)))->find();

		//如果查询成功，则标记为已读
		if($message && $message['status'] === '1') {
			$messageModel->where(array('id' => $id))->save(array('status' => 2));
			$message['status'] = 2;
		}
		
		$this->ajaxReturn($this->result($message));
	}
	
	/**
	 * 查询未读消息数量
	 */
	public function unreadCount()
	{
		$unread = $messageModel->unreadMessageCount($this->user['userId']);
		$this->ajaxReturn(array('error' => 0, 'info' => $unread));
	}
}